<?php
namespace app\first\model;

use think\Db;
use think\Model;
/**
 * Class Push
 * @package app\push\validate
 */
class ArticleLove extends Model{
    protected $autoWriteTimestamp = false;

    /**
     * @param array $where
     * @param string $order
     * @param int $limit
     * @param string $filed
     * @param bool $out
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where=[],$order='create_time desc',$limit=10,$filed='*',$out=false){
        $list = $this
            ->field($filed,$out)
            ->with(['user'=>function($query){
                $query->field('id,account,nickname,sex,avatar,is_auth,is_vip,vip_type');
            },'article'=>function($query){
                $query->field('id,title');
            }])
            ->where($where)
            ->order($order)
            ->paginate($limit);
        return $list;
    }

    public function user(){
        return $this->hasOne('member','id','uid');
    }

    public function article(){
        return $this->hasOne('Article','id','aid');
    }
    /**
     * 点赞
     * @param int $aid
     * @param int $uid
     * @param int $type
     * @param string $out
     * @return bool
     */
    public function addNew($aid=0,$uid=0,$type=1,&$out=''){
        $data = [
            'uid'=>$uid,
            'aid'=>$aid,
            'type'=>$type,
            'create_time'=> time()
        ];
        $count = $this->where('aid','eq',$uid)->count('id');
        $info = (new Article())->find($aid);
        if(!$info){
            $out = '点评文章不存在';
            return false;
        }
        if($count){
            $out = '您已经点评了';
            return false;
        }
        if($type==1){
            $info->love = $info['love'] +1;
        }else{
            $info->bad = $info['bad'] +1;
        }
        $valid = new \app\first\validate\ArticleLove();
        if(!$valid->check($data)){
            $out = $valid->getError();
            return false;
        }
        Db::startTrans();
        $result = $info->save();
        $result1 = $this->save($data);
        if($result1 && $result){
            Db::commit();
            return true;
        }else{
            $out = '操作失败';
            Db::rollback();
            return false;
        }
    }

    /**
     * @param int $uid
     * @param int $aid
     * @return float|string
     */
    public function isYelp($uid=0,$aid=0){
        $count = $this
            ->where('uid','eq',$uid)
            ->where('aid','eq',$aid)
           ->where('type','eq',1)
            ->count('1');
        $count1 = $this
            ->where('uid','eq',$uid)
            ->where('aid','eq',$aid)
            ->where('type','eq',2)
            ->count('1');
        if($count){
            return 1;
        }elseif($count1) {
            return 2;
        }else{
            return 0;
        }
    }

    /**
     * @param $uid
     * @param $aid
     * @param $type
     * @param string $out
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function delYelp($uid,$aid,$type,&$out=''){
        $info = (new Article())->find($aid);
        $info1 = $this
            ->where('aid','eq',$aid)
            ->where('uid','eq',$uid)
            ->where('type','eq',$type)
            ->find();
        if(!$info && !$info1){
            $out = '点评不存在';
            return false;
        }
        if($type==1){
            $info->love = $info['love'] - 1;
        }else{
            $info->bad = $info['bad'] - 1;
        }
        Db::startTrans();
        $result = $info->save();
        $result1 = $info1->delete();
        if($result && $result1){
            Db::commit();
            return true;
        }else{
            $out = '取消失败';
            return true;
        }
    }
}